package com.lsk.task;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.weixin.sdk.api.AccessToken;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.cache.IAccessTokenCache;
import com.jfinal.weixin.sdk.kit.ParaMap;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.RetryUtils;
import com.lsk.bean.WxBean;
import com.lsk.util.GlobalParameter;
import com.lsk.util.Syslog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 刷新微信公众号token
 * Created by Administrator on 2018/8/2 0002.
 */
public class RefreshWxToken {
    Cache cache= Redis.use();
    private static String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    private static Map<String, String> queryParas=null;
    public RefreshWxToken(){

        if (GlobalParameter.windows==false){
            String sql="SELECT *from weixin_public_account_info where type=0 GROUP BY appId";
            List<Record> recordList=new ArrayList<>();
            WxBean wxBean=new WxBean();
            Record base=new Record().set("appId",wxBean.getAppId()).set("appsecret",wxBean.getAppSecret())
                    .set("originalID",wxBean.getOriginalId()).set("mch_id",wxBean.getMchId())
                    .set("payKey",wxBean.getPayKey()).set("domain",wxBean.getDomain());
            recordList.add(0,base);
            for (int i=0;i<recordList.size();i++){
                try {
                    Record tempRecord=recordList.get(i);
                    String appId=tempRecord.getStr("appId");
                    String appsecret=tempRecord.getStr("appsecret");
                    String originalID=tempRecord.getStr("originalID");
                    String orgId=tempRecord.getStr("orgId");
                    cache.set(appId,tempRecord);
                    final Map<String, String> queryParas = ParaMap.create("appid", appId).put("secret", appsecret).getData();
                    RefreshWxToken.queryParas=queryParas;
                    AccessToken result = (AccessToken) RetryUtils.retryOnException(3, new Callable<AccessToken>() {
                        public AccessToken call() throws Exception {
                            String json = HttpUtils.get(RefreshWxToken.url, RefreshWxToken.queryParas);
                            return new AccessToken(json);
                        }
                    });
                    String token=result.getAccessToken();
                    String rule="wxToken";
                    cache.set(rule+appId,token);
                    cache.set(rule+originalID,token);
                    cache.set(rule+orgId,token);
                    IAccessTokenCache accessTokenCache = ApiConfigKit.getAccessTokenCache();
                    accessTokenCache.set(appId,result.getCacheJson());
                    Syslog.info("获取到的token信息" +result.getJson());
                }catch (Exception e){
                    Syslog.error(e);
                }finally {
                    RefreshWxToken.queryParas=null;
                }
            }
        }

    }
}
